<?php
/**
 * 获取测试数据的类
 */

namespace App\Http\Controllers\Es;
use App\Http\Controllers\Controller;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Pool;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Exception\RequestException;

class GetDataController extends Controller
{
    public function getSingleRequest($page)
    {
        $base_uri = 'http://v.juhe.cn';
        $client = new Client(array('base_uri' => $base_uri));
        $result = array();
        foreach ($page as $single_page)
        {
            $options = array(
                'query' =>[
                    'type' => 'top',
                    'key' => '4305d3e32bb911643a034f733032978b',
                    'page' => $single_page
                ]
            );
            $data = $client->request('get','/toutiao/index',$options)->getBody()->getContents();
            $result[$single_page] = array();
            if(!empty($data))
            {
                $data_temp = json_decode($data,true);
                if(isset($data_temp['error_code'])&&($data_temp['error_code'] == 0))
                {
                    if(isset($data_temp['result']['data'])&&!empty($data_temp['result']['data']))
                    {
                        $result[$single_page] = $data_temp['result']['data'];
                    }
                }
            }
        }
        return $result;
    }

    public function getBulkRequest($page)
    {
        $base_uri = 'http://v.juhe.cn/toutiao/index?';
        $client = new Client();
        $requests = function ($page) use ($base_uri) {
            for ($i = 1;$i <= $page; $i++) {
                $query = array(
                    'type' => 'top',
                    'key' => '4305d3e32bb911643a034f733032978b',
                    'page' => $i
                );
                $base_uri .= http_build_query($query);
                yield new Request('GET', $base_uri);
            }
        };
        $result = array();
        $pool = new Pool($client, $requests($page), [
            'concurrency' => 5,
            'fulfilled' => function (Response $response, $index) use(&$result) {
                $result[$index] = array();
                $contents = $response->getBody()->getContents();
                $contents_temp = json_decode($contents, true);
                if(isset($contents_temp['error_code'])&&($contents_temp['error_code'] == 0))
                {
                    if(isset($contents_temp['result']['data'])&&!empty($contents_temp['result']['data']))
                    {
                        $result[$index] = $contents_temp['result']['data'];
                    }
                }
            },
            'rejected' => function (RequestException $reason, $index) {
                $result[$index] = [];
            },
        ]);
        $promise = $pool->promise();
        $promise->wait();
        return $result;
    }

    public function getData()
    {
        for($i= 0;$i<=30;$i++)
        {
            $page_range[] = $i;
        }
        $single_request_start = microtime(true);
        $new_list = $this->getSingleRequest($page_range);
        $single_request_end = microtime(true);
        $single_request_diff = round((($single_request_end - $single_request_start) / 1000),4);
        echo '单次分页查询用时：'.$single_request_diff.'秒';

        $bulk_request_start = microtime(true);
        $new_list_bulk = $this->getBulkRequest(30);
        $bulk_request_end = microtime(true);
        $bulk_request_diff = round((($bulk_request_end-$bulk_request_start) / 1000),4);
        echo '批量查询用时：'.$bulk_request_diff.'秒';
    }
}